
#include <array>
#include <cassert>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using json = nlohmann::json;

/**
 * @brief 测试空的结构体
 *
 */

struct Null {};
void to_json(json &js, const Null &db) { js = "{}"_json; }
void from_json(const json &js, Null &db) {}

struct Basic {
  virtual void to_json(json &js) const {}
  virtual void from_json(const json &js) {}
};
void to_json(json &js, const Basic &db) { db.to_json(js); }
void from_json(const json &js, Basic &db) { db.from_json(js); }

struct A : public Basic {
  int a{1};
  string a_name{"a"};
  void to_json(json &js) const override { js = {{"a", a}, {"a_name", a_name}}; }
  void from_json(const json &js) override {
    a = js["a"].get<int>();
    a_name = js["a_name"].get<string>();
  }
};

struct B : public Basic {
  int b{2};
  string b_name{"b"};
  void to_json(json &js) const override { js = {{"b", b}, {"b_name", b_name}}; }
  void from_json(const json &js) override {
    b = js["b"].get<int>();
    b_name = js["b_name"].get<string>();
  }
};

struct C {
  int c{3};
  string c_name{"c"};
  void to_json(json &js) const { js = {{"c", c}, {"c_name", c_name}}; }
  void from_json(const json &js) {
    c = js["c"].get<int>();
    c_name = js["c_name"].get<string>();
  }
};

struct D : public Basic {
  int d{4};
  string d_name{"d"};
  int c;
  void to_json(json &js) const { js = *this; }
  void from_json(const json &js) { *this = js; }
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(D, d, d_name)

/**
 * @brief 测试把注释从json文件中删除
 *
 * @return int
 */
int main() {

  // 测试空的结构体
  Null test;
  json js = test;
  cout << js << endl;

  // 测试多态
  Basic *ptr;
  A a;
  ptr = &a;
  json jsa = *ptr;
  cout << jsa << endl;

  B b;
  ptr = &b;
  json jsb = *ptr;
  cout << jsb << endl;

  // 这个也行
  B bb;
  json jsbb = bb;
  cout << jsbb << endl;

  // 这个不行
  //   C c;
  //   json jsc = c;
  //   cout << jsc << endl;

  D d;
  ptr = &d;
  json jsd = *ptr;
  cout << jsd << endl;

  // 测试array
  ifstream input{"input4.json"};
  json j;
  input >> j;
  cout << j << endl;
  json arr = j["arr"];
  for (auto iter = arr.begin(); iter != arr.end(); ++iter)
    cout << "\n\n" << *iter << "\n\n" << endl;
  for (auto &elem : arr)
    cout << elem << endl;
  return 0;
}